<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/27/21
 * Time: 11:40 PM
 */

namespace app\api\controller\v1;


use app\api\model\Product as ProductModel;
use app\api\model\ProductOrder;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use app\api\model\UserInvitation;

class ProductNum
{
    // 产品销量
    public function getProductNumber($start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }

        $productList = ProductModel::with('store')->select()->toArray();
        $array = [];
        foreach ($productList as $k=>$v){
            $productList[$k]['count'] = 0;
            $whereTwo = $where;
            $whereTwo[] = ['product_order.status','gt',1];
            $product_id = $productList[$k]['id'];
            $order = ProductOrder::hasWhere('content',function ($query) use($product_id){
                $query->where('product_id','eq',$product_id);
            })->where($whereTwo)->with('content')->select()->toArray();
            foreach ($order as $ka=>$va){
                $productList[$k]['count'] += $order[$ka]['content'][0]['product_number'];
            }
            if($productList[$k]['count'] > 0) {
                array_push($array,$productList[$k]);
            }
        }
        if(count($array) > 0) {
            $array = arraySort($array,'count');
        }
        return $array;
    }

    // 产品销量
    public function getProductOrderList($product_id,$start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $whereTwo = $where;
        $whereTwo[] = ['product_order.status','gt',1];
        $list = ProductOrder::hasWhere('content',function ($query) use($product_id){
            $query->where('product_id','eq',$product_id);
        })->where($whereTwo)->with('store,user,content')->order('pay_time desc')->select();
        if(!$list->isEmpty()) {
            foreach ($list->toArray() as $k=>$v){
                $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
                if($invite['type'] == 1) {
                    $list[$k]['invite'] = UserModel::get($invite['inviter']);
                }elseif($invite['type'] == 2){
                    $store_user = StoreUserModel::get($invite['inviter']);
                    $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                    $list[$k]['invite']['nickname'] = $store_user['name'];
                    $list[$k]['invite']['phone'] = $store_user['phone'];
                }elseif($invite['type'] == 3){
                    $team_user = TeamUserModel::get($invite['inviter']);
                    $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                    $list[$k]['invite']['nickname'] = $team_user['name'];
                    $list[$k]['invite']['phone'] = $team_user['phone'];
                }
                $list[$k]['pay_time_int'] = strtotime($list[$k]['pay_time']);
            }
        }
        return $list->toArray();
    }
}